import os


class CourseManage:
    def __init__(self):
        pass
# 主菜单
    def menu(self):
        print("=" * 50)
        print("---------------课程管理系统---------------")
        print("1、添加课程信息")
        print("2、删除课程信息")
        print("3、查询课程信息")
        print("4、保存课程信息数据")
        print("5、打开课程信息数据")
        print("0、退出系统")
        print("=" * 50)
# 添加课程功能
    def tianjia(self):
        print('---------添加功能---------')
        new_id = input("请输入课程编号:")
        new_type = input("请输入课程类型:")
        new_score = input("请输入课程学分:")
        new_period = input("请输入课程课时:")
        new_name = input("请输入课程名字:")
        new_dict = {}
        new_dict['id'] = new_id
        new_dict['type'] = new_type
        new_dict['score'] = new_score
        new_dict['period'] = new_period
        new_dict['name'] = new_name
        course_list.append(new_dict)
        print("添加完成！")
        print('课程编号：{}\t课程类型：{}\t课程学分：{}\t课程课时：{}\t课程名称：{}'.format(
            new_dict['id'], new_dict['type'], new_dict['score'], new_dict['period'], new_dict['name']))
        i = 1
        while i == 1:
            num = input("继续？(y/n)")
            if num == 'y':
                break
            elif num == 'n':
                exit()
            else:
                print("输入错误！")
# 删除课程功能
    def shanchu(self):
        print('---------删除功能---------')
        id = input("请输入要删除课程的编号：")
        for temp in course_list:
            if temp['id'] == id:
                course_list.remove(temp)
                print("删除完成")
                print("-------剩余数据-------")
                for temp1 in course_list:
                    print('课程编号：{}\t课程类型：{}\t课程学分：{}\t课程课时：{}\t课程名称：{}'.format(
                        temp1['id'], temp1['type'], temp1['score'], temp1['period'], temp1['name']))
                num = input("删除完成，是否继续？(y/n)")
                if num == 'y':
                    break
                else:
                    exit()
        else:
            num = input("未找到，是否继续？(y/n)")
            if num == 'y':
                pass
            else:
                exit()
# 查询课程功能
    def chaxun(self):
        print('---------查询功能---------')
        id = input("请输入要查询的课程编号：")
        for temp in course_list:
            if temp['id'] == id:
                print("-------查询结果-------")
                print('课程编号：{}\t课程类型：{}\t课程学分：{}\t课程课时：{}\t课程名称：{}'.format(
                    temp['id'], temp['type'], temp['score'], temp['period'], temp['name']))
                num = input("继续？(y/n)")
                if num == 'y':
                    break
                else:
                    exit()
        else:
            num = input("未找到，是否继续？(y/n)")
            if num == 'y':
                pass
            else:
                exit()
# 保存课程列表功能
    def baocun(self):
        print('---------保存功能---------')
        with open('course.txt', 'w+')as file1:
            for temp in course_list:
                file1.write('课程编号：{}\t课程类型：{}\t课程学分：{}\t课程课时：{}\t课程名称：{}\n'.format(
                    temp['id'], temp['type'], temp['score'], temp['period'], temp['name']))
            print("保存数据成功！")
            num = input("继续？(y/n)")
            if num == 'y':
                pass
            elif num == 'n':
                exit()

    def dakai(self):
        print('---------打开功能---------')
        with open('course.txt', 'r')as file2:
            content = file2.read()
            print(content)

    def tuichu(self):
        exit()

# 调用功能
course_list = []
cm = CourseManage()
while 1:
    cm.menu()
    num = (input("请输入功能编号："))
    if num == '1':
        cm.tianjia()
    elif num == '2':
        cm.shanchu()
    elif num == '3':
        cm.chaxun()
    elif num == '4':
        cm.baocun()
    elif num == '5':
        cm.dakai()
    elif num == '0':
        cm.tuichu()
    else:
        print("无法突破次元壁")
        dd = input("重新来！（y/n）")
        if dd == 'y':
            pass
        else:
            exit()
